package rxjava;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;

public class RxJavaTest {
    public static void main(String[] args) {
        e1();
    }

    private static void e1() {
        Observer<String> observer = new Observer<String>() {

            @Override
            public void onSubscribe(@NonNull Disposable d) {
                System.out.println("onSubscribe()");
            }

            @Override
            public void onNext(@NonNull String s) {
                System.out.println("on next:" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("on complete");
            }
        };

        Observable<String> observable = new Observable<String>() {

            @Override
            protected void subscribeActual(@NonNull Observer<? super String> observer) {
                observer.onSubscribe(new Disposable() {
                    @Override
                    public void dispose() {

                    }

                    @Override
                    public boolean isDisposed() {
                        return false;
                    }
                });
                observer.onNext("2");
                observer.onComplete();
            }
        };

        Observable<String> dd = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
                emitter.onNext("dd");
                emitter.onComplete();
            }
        });


        Observable<String> map = dd.map(new Function<String, String>() {
            @Override
            public String apply(String s) throws Throwable {
                return s + "====##";
            }
        });

        Observable<Integer> integerObservable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
                emitter.onNext(999);
            }
        });

        Observable<String> map1 = integerObservable.map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Throwable {
                return integer + ",,";
            }
        });

//        map.subscribe(observer);

        map1.subscribe(observer);
    }
}
